package jwj8508.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingPopupMenu extends JPopupMenu{		
	
	private JPopupMenu popMenu = null;
	private JMenu mnFile = null;

	public SwingPopupMenu(JPanel p) {
		makePopMenu();
		p.addMouseListener(new PopupMenuListener());
		// SwingMain의 contentPane(JPanel)에
		// PopupMenuListener를 MouseListener로 등록		
		makeMenu();
		addPopMenu();		
		
	}	
	
	private void makePopMenu() {
		popMenu = new JPopupMenu();
	}
	
	private void makeMenu() {
		mnFile = new JMenu("File (F)");
		JMenuItem mm = new JMenuItem("n");
		mnFile.add(mm);
	}
	
	private void addPopMenu(){
		popMenu.add(mnFile);
		
	}	
	
	class PopupMenuListener extends MouseAdapter {		
		@Override
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		
		private void showMenu(MouseEvent e) {
			popMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}
}
